﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Negro
{
    public partial class Entrada : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (Session["ENTRADA_FORM"] != null)
                {
                    Entrada entrada = (Entrada)Session["ENTRADA_FORM"];

                    this.lista.DataSource = entrada.lista.DataSource;

                }
            }


        }

        protected void btnAgregar_Click(object sender, EventArgs e)
        {

            EntradaBusiness business = new EntradaBusiness();

            ProductosDTO bean = business.consulta(this.claveProcucto.Text);

            if (bean != null)
            {

                DataTable dataTable = (DataTable)this.lista.DataSource;

                List<ProductosDTO> lista = new List<ProductosDTO>();

                if (dataTable != null)
                {

                    List<ProductosDTO> listaTotal = Util.Convert.ToCollection<ProductosDTO>(dataTable);

                    int index = 0;

                    foreach (GridViewRow item in this.lista.Rows)
                    {

                        TextBox txt = (TextBox)item.FindControl("txtCantidad");

                        long valor = Convert.ToInt64(txt.Text);


                        ProductosDTO dto = listaTotal.ElementAt(index);

                        dto.Cantidad = valor;

                        index++;

                        lista.Add( dto );

                    }



                    /*

                    foreach (ProductosDTO item in listaTotal)
                    {
                        ProductosDTO aux = item;

                        lista.Add(aux);
                    }*/

                }

                lista.Add(bean);

                dataTable = Util.Convert.ToDataTable<ProductosDTO>(lista.ToList());

                this.lista.DataSource = dataTable;

                this.lista.DataBind();

                Session["ENTRADA_FORM"] = this;
            }
        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            if (Session["ENTRADA_FORM"] != null)
            {

                DataTable dataTable = (DataTable)this.lista.DataSource;

                List<ProductosDTO> listaTotal = Util.Convert.ToCollection<ProductosDTO>(dataTable);

                int index = 0;

                foreach (GridViewRow item in this.lista.Rows)
                {

                    TextBox txt = (TextBox)item.FindControl("txtCantidad");

                    long valor = Convert.ToInt64( txt.Text ) ;


                    ProductosDTO dto = listaTotal.ElementAt(index);

                    dto.Cantidad = valor;
                   
                    index++;

                }

                if (dataTable != null)
                {
               
                    EntradaBusiness business = new EntradaBusiness();

                    business.guardar(listaTotal);

                }

            }
        }

        protected void btnRegresar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/aspx/Menu.aspx");
        }
    }
}